package com.example.experiments.filebrowser;

import java.io.File;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

public class MyTableContentProvider implements ITreeContentProvider{

	private File[] roots;
	
	@Override
	public void dispose() {
		
	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		roots = (File[]) newInput;
	}

	@Override
	public Object[] getElements(Object inputElement) {
		return roots;
	}

	@Override
	public Object[] getChildren(Object parentElement) {
		if ( parentElement == null)
			System.out.println( "parentElement = " + parentElement);
		File parent = (File) parentElement;
			
		return parent.listFiles();
	}

	@Override
	public Object getParent(Object element) {
		if ( element == null)
			System.out.println( "element = " + element);
		File elm = (File) element;
		return elm.getParentFile();
	}

	@Override
	public boolean hasChildren(Object element) {
		if ( element == null)
			System.out.println( "element = " + element);
		File dir = (File) element;
		if ( dir.isDirectory()) {
			File[] files = dir.listFiles();
			if ( files != null && files.length > 0)
				return true;
		}
		return false;
	}

}
